
//主进程
const {app, BrowserWindow, screen, ipcMain, Menu, nativeImage, Tray,globalShortcut} = require("electron")
const path = require("path");

//定义菜单模板数据
const menuTemplate = [
    {
        label: "文件",
        submenu: [
            {
                label: "新建",
                accelerator: "Shift + N",
            },
            {
                label: "打开",
                accelerator: "Shift + O",
            }
        ]
    },
    {
        label: "编辑",
        submenu: [
            {
                label: "剪切",
                accelerator: "Ctrl + C",
                click: () => {
                    console.log("剪切")
                }
            },
            {
                label: "复制"
            },
            {type: 'separator'},
            {
                label: "撤销"
            },
            {
                label: "重做"
            }
        ]
    }
];
//创建菜单模板
const menu = Menu.buildFromTemplate(menuTemplate)

//托盘图标
let icon = nativeImage.createFromPath("logo1.png");
icon = icon.resize({width: 1000, height: 1000})

const appIcon = nativeImage.createFromPath('logo1.png')

//2.创建窗口
const createWindow = () => {
    // console.log(screen.getAllDisplays())
    const primaryDisplay = screen.getPrimaryDisplay();
    // console.log(primaryDisplay)
    // const { width, height } = primaryDisplay.workAreaSize
    // console.log( width, height)

    //3.创建BrowserWindow实例对象
    const win = new BrowserWindow({
        width: 1130,
        height: 732,
        title: "微信",
        icon: appIcon,
        frame: false,
        webPreferences: {
            // nodeIntegration: true, //开启node 环境
            // contextIsolation: false, //开通上下文环境 即渲染进程可以获取主进程的模块
            preload: path.join(__dirname, "/preload.js")
        }
    });


    //加载http 路径
    // win.loadURL("https://wwww.baidu.com");
    //加载本地文件
    win.loadFile("index.html")

    // win.webContents.openDevTools()
    //设置自定义导航菜单
    Menu.setApplicationMenu(menu)

    let tray = new Tray(icon);
    tray.setToolTip('微信')
    const contextMenu = Menu.buildFromTemplate([
        {label: '设置'},
        {label: '意见反馈'},
        {label: '帮助'},
        {label: '关于微信'},
        {label: '退出微信'}
    ])
    tray.setContextMenu(contextMenu)

    //注册全局快捷键
    globalShortcut.register("Enter",()=>{
        console.log("收款");
    })

}
//关闭所有窗口
app.on("window-all-closed", () => {
    console.log("window-all-closed")
    if (process.platform !== "darwin") { // Electron 目前只支持三个平台：win32 (Windows), linux (Linux) 和 darwin (macOS)
        app.quit();
    }
})


app.on('browser-window-focus',()=>{
    if (!globalShortcut.isRegistered('Enter')){
        globalShortcut.register("Enter",()=>{
            console.log("收款");
        })
    }
})


app.on('browser-window-blur',()=>{
    globalShortcut.unregister('Enter')
})

//1.监听app的ready 事件之后，再创建浏览器窗口
app.whenReady().then(() => {

    //4.调用创建窗口方法
    createWindow()

    app.on("activate", () => {
        if (BrowserWindow.getAllWindows().length === 0) {
            createWindow();
        }
    })
})


//监听主进程 单向通信
ipcMain.on('send-msg', (event, args) => {
    console.log(args)
})
//监听主进程 双向通信
/*ipcMain.handle('send-msg',(event, args)=>{
    console.log(args)
    return args + '456'
})*/


